package com.diaohw.platform.framework.db.flex;

import com.diaohw.platform.common.enums.CodeMsgEnum;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;

/**
 * 数据插入校验
 */
public class DataCheckUtils {

    /**
     * 插入校验
     *
     * @param column 字段
     * @param value  字段值
     * @param mapper BaseMapper
     * @param <R>    Entity类
     * @param <T>    字段类型
     */
    public static <R, T> void insert(QueryColumn column, T value, BaseMapper<R> mapper) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(column.eq(value));
        long count = mapper.selectCountByCondition(condition);
        throwException(count, value);
    }

    /**
     * 数据更新校验
     *
     * @param idColumn 主键ID字段
     * @param id       主键 Value
     * @param column   更新字段
     * @param value    更新Value
     * @param mapper   BaseMapper
     * @param <R>      Entity类
     * @param <T>      字段类型
     */
    public static <R, T> void update(QueryColumn idColumn, String id, QueryColumn column, T value, BaseMapper<R> mapper) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(column.eq(idColumn.getName()))
                .and(idColumn.eq(id));
        long count = mapper.selectCountByCondition(condition);
        throwException(count, value);
    }

    /**
     * 插入校验
     *
     * @param restrictive      前置字段
     * @param restrictiveValue 前置字段Value
     * @param column           字段
     * @param value            字段值
     * @param mapper           BaseMapper
     * @param <R>              Entity类
     * @param <T>              字段类型
     */
    public static <T, R> void restrictiveInsert(QueryColumn restrictive, String restrictiveValue, QueryColumn column, T value, BaseMapper<R> mapper) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(column.eq(value))
                .and(restrictive.eq(restrictiveValue));

        long count = mapper.selectCountByCondition(condition);
        throwException(count, value);
    }

    /**
     * 更新校验
     *
     * @param id               主键ID字段
     * @param idValue          主键 Value
     * @param restrictive      前置字段
     * @param restrictiveValue 前置字段Value
     * @param column           字段
     * @param value            字段值
     * @param mapper           BaseMapper
     * @param <R>              Entity类
     * @param <T>              字段类型
     */
    public static <T, R> void restrictiveUpdate(QueryColumn id, String idValue, QueryColumn restrictive, String restrictiveValue, QueryColumn column, T value, BaseMapper<R> mapper) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(column.eq(value))
                .and(id.ne(idValue))
                .and(restrictive.eq(restrictiveValue))
                .and(column.eq(value));

        long count = mapper.selectCountByCondition(condition);
        throwException(count, value);
    }


    public static void throwException(Long count, Object value) {
        if (count > 0) {
            throw CodeMsgEnum.DATA.build(String.format("参数值[%s]重复", value));
        }
    }

}
